package com.example.myapplication2;


import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication2.dataBase.DBHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DataBaseCRUDActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText nameEdit4;
    private EditText idEdit4;
    private EditText ageEdit4;
    private EditText heightEdit4;
    private CheckBox checkBox;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_base_crudactivity);
        nameEdit4 = findViewById(R.id.nameEdit4);
        idEdit4 = findViewById(R.id.IdEdit4);
        ageEdit4 = findViewById(R.id.ageEdit4);
        heightEdit4 = findViewById(R.id.heightEdit4);
        checkBox = findViewById(R.id.checkBox4);
        //注册监听事件
        findViewById(R.id.dataBaseAdd).setOnClickListener(this);
        findViewById(R.id.dataBaseUpdate).setOnClickListener(this);
        findViewById(R.id.dataBaseDelete).setOnClickListener(this);
        findViewById(R.id.dataBaseQuery).setOnClickListener(this);
        findViewById(R.id.dataBaseQueryAll).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //新增
            case R.id.dataBaseAdd:
                Map<String, Object> map=new HashMap<>();
                map.put("name",nameEdit4.getText().toString());
                map.put("age",ageEdit4.getText().toString());
                map.put("height",heightEdit4.getText().toString());
                map.put("checkBox",checkBox.isChecked());
                long insert = dbHelper.insert(map);
                if (insert!=-1){
                    idEdit4.setText(insert+"");
                    showInfo("添加成功");
                }else {
                    showInfo("添加失败");
                }
                break;
                //修改
            case R.id.dataBaseUpdate:
                Map<String, Object> map2=new HashMap<>();
                map2.put("name",nameEdit4.getText().toString());
                map2.put("id",idEdit4.getText().toString());
                map2.put("age",ageEdit4.getText().toString());
                map2.put("height",heightEdit4.getText().toString());
                map2.put("checkBox",checkBox.isChecked());
                long update = dbHelper.update(map2);
                break;
                //删除
            case R.id.dataBaseDelete:
                String s = idEdit4.getText().toString();
                int delete = dbHelper.delete(Long.valueOf(s));
                if (delete>0){
                    showInfo("操作成功:"+s);
                }else {
                    showInfo("添加失败");
                }
                break;
                //搜索
            case R.id.dataBaseQuery:

                Map find = dbHelper.find(Long.valueOf(idEdit4.getText().toString()));
                if (find.isEmpty()){
                    showInfo("记录为空");
                }else {
                    nameEdit4.setText(find.get("name").toString());
                    ageEdit4.setText(find.get("age").toString());
                    heightEdit4.setText(find.get("height").toString());
                    checkBox.setChecked((Boolean) find.get("checkBox"));
                }
                break;
                //搜索所有
            case R.id.dataBaseQueryAll:
                List<Map<String, Object>> all = dbHelper.findAll();
                for (Map<String, Object> stringObjectMap : all) {
                    Set<String> strings = stringObjectMap.keySet();
                    for (String string : strings) {
                        System.out.println(stringObjectMap.get(string));
                    }
                    System.out.println("..............");
                }
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        dbHelper.closeLink();
    }

    @Override
    protected void onStart() {
        super.onStart();
        //获取实例,打开连接
        dbHelper = DBHelper.getInstance(this);
        dbHelper.openReadLink();
        dbHelper.openWriteLink();
    }
    void showInfo(String info){
        Toast.makeText(this,info,Toast.LENGTH_SHORT).show();
    }
}